package com.ptswitch.material.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ptswitch.material.dao.RoleMapper;
import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.service.RoleService;
import com.ptswitch.material.util.security.RoleUtil;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper mapper;

    @Override
    public void initRoles() {
        long count = mapper.countAll();
        if (count == 0) {
            addAll(RoleUtil.roles);
        } else {
            addRelateRole();
        }
    }

    private void addRelateRole() {
        List<Role> dbRoles = findByRoleGroup(RoleUtil.RoleGroupName_Relate);
        Map<String, Role> dbRoleMap = new HashMap<String, Role>();
        for (Role dbRole : dbRoles) {
            dbRoleMap.put(dbRole.getRoleName(), dbRole);
        }
        for (Role r : RoleUtil.roles) {
            if (RoleUtil.RoleGroupName_Relate.equals(r.getRoleGroup())) {
                Role dbRole = dbRoleMap.get(r.getRoleName());
                if (dbRole == null) {
                    mapper.add(r);
                } else {
                    r.setId(dbRole.getId());
                }
            }
        }
    }

    //
    @Override
    public List<Uzer> listUserByRoleInfo(String roleId, String username, boolean in, long first, long size) {
        if (in) {
            return mapper.listUserHaveRole(roleId, username, first, size);
        }
        return mapper.listUserNotHaveRole(roleId, username, first, size);
    }

    @Override
    public long countUserByRoleInfo(String roleId, String username, boolean in) {
        if (in) {
            return mapper.countUserHaveRole(roleId, username);
        }
        return mapper.countUserNotHaveRole(roleId, username);
    }

    //
    @Override
    public List<Role> findRoles(Role... roles) {
//        if (roles.length == 0) {
//            return Collections.emptyList();
//        }
//        StringBuilder whereHql = new StringBuilder(" where p.roleName in (");
//        for (Role role : roles) {
//            whereHql.append("'").append(role.getRoleName()).append("'").append(",");
//        }
//        whereHql.append("''").append(")");
//        return dao.list(Role.class, whereHql.toString(), -1, -1);
        return null;
    }

    @Override
    public List<Role> findRoles(List<Role> roles) {
        return findRoles(roles.toArray(new Role[roles.size()]));
    }

    @Override
    public List<Role> findByRoleGroup(String groupName) {
        return mapper.findByRoleGroup(groupName);
    }

    @Override
    public List<Role> findNormalRoles() {
        return mapper.findNormalRoles();
    }

    @Override
    public List<Role> findRelateRoles() {
        return mapper.findRelateRoles();
    }

    @Override
    public Role findById(String id) {
        return mapper.findById(id);
    }

    @Override
    public void save(Role role) {
        long id = mapper.add(role);
        role.setId(id);
    }

    private void addAll(Collection<Role> roles) {
        for (Role role : roles) {
            save(role);
        }
    }

    @Override
    public List<Role> findAll(long first, long size) {
        return mapper.findAll();
    }
}
